package at.sysarch.filter;

import java.awt.image.renderable.ParameterBlock;

import javax.media.jai.JAI;
import javax.media.jai.KernelJAI;
import javax.media.jai.PlanarImage;

import at.pimpmypipe.filter.DataEnrichmentFilter;
import at.sysarch.domain.TransferImage;
import at.sysarch.utils.KernelHelper;

public class OpeningFilter extends DataEnrichmentFilter<TransferImage, TransferImage> {
	
	private KernelJAI _erodeKernel = KernelHelper.getKernel(12, 12);
	private KernelJAI _dilateKernel = KernelHelper.getKernel(12, 12);

	@Override
	protected boolean fillEntity(TransferImage nextVal, TransferImage entity) {
		
		entity.setSourceImage(nextVal.getSourceImage());
		
		ParameterBlock erodeParams = new ParameterBlock();
		
		erodeParams.addSource(nextVal.getModifiedImage());
		erodeParams.add(_erodeKernel);
		
		PlanarImage image = JAI.create("erode", erodeParams);
		
		
		ParameterBlock dilateParams = new ParameterBlock();
		
		dilateParams.addSource(image);
		dilateParams.add(_dilateKernel);
		
		entity.setModifiedImage(JAI.create("dilate", dilateParams));

		return true;
	}

	@Override
	protected TransferImage getNewEntityObject() {

		return new TransferImage();
	}

	public void setErodeKernel(KernelJAI erodeKernel) {
		_erodeKernel = erodeKernel;
	}

	public void setDilateKernel(KernelJAI dilateKernel) {
		_dilateKernel = dilateKernel;
	}
}
